
package com.ami.kvm.jviewer.communication;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.LocaleStrings;

import javax.net.ssl.*;
import javax.swing.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class Connection {
	public static int SUCCESS = 0;
	public static int INVALID_SOCKET = -1;
	public static int SOCK_CREAT_FAILED = -2;

	private boolean validateSSLCert = true;
	private boolean webSSLVerfiy = true;
	private boolean kvmSSLVerify = true;
	private int connErrCode = INVALID_SOCKET;
    public static final int SOCKET_CONNECT_TIMEOUT = 90000;

	public SSLSocketFactory getSocketFactory() {
		Object localObject = null;
		SSLSocketFactory localSSLSocketFactory = null;
		try {
			TrustManager[] arrayOfTrustManager = { new X509TrustManager() {
				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}

				public void checkClientTrusted(X509Certificate[] paramAnonymousArrayOfX509Certificate,
						String paramAnonymousString) {
				}

				public void checkServerTrusted(X509Certificate[] paramAnonymousArrayOfX509Certificate,
						String paramAnonymousString) {
				}
			} };
			SSLContext localSSLContext = SSLContext.getInstance("SSL");
			localSSLContext.init(null, arrayOfTrustManager, new SecureRandom());
			localObject = localSSLContext;
			localSSLSocketFactory = ((SSLContext) localObject).getSocketFactory();

		} catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
			Debug.out.println(localNoSuchAlgorithmException);
		} catch (KeyManagementException localKeyManagementException) {
			Debug.out.println(localKeyManagementException);
		}
		return localSSLSocketFactory;
	}

	private SSLSocket createSSLSocket(InetAddress paramInetAddress, int paramInt1, int paramInt2) {
		SSLSocket localSSLSocket = null;
		int i = 1;

		int j = 0;

		if (paramInt2 == 1) {
			this.validateSSLCert = this.webSSLVerfiy;
		} else
			this.validateSSLCert = this.kvmSSLVerify;
		SSLSocketFactory localSSLSocketFactory1;
		if (this.validateSSLCert == true) {

			SSLSocketFactory localSSLSocketFactory2 = (SSLSocketFactory) SSLSocketFactory.getDefault();
			HttpsURLConnection.setDefaultSSLSocketFactory(localSSLSocketFactory2);
			localSSLSocketFactory1 = HttpsURLConnection.getDefaultSSLSocketFactory();
			try {
				localSSLSocket = (SSLSocket) localSSLSocketFactory1.createSocket(paramInetAddress, paramInt1);
			} catch (UnknownHostException localUnknownHostException2) {
				Debug.out.println(localUnknownHostException2);
				localSSLSocket = null;
			} catch (IOException localIOException2) {
				Debug.out.println(localIOException2);
				localSSLSocket = null;
			} catch (NullPointerException localNullPointerException) {
				Debug.out.println(localNullPointerException);
				localSSLSocket = null;
			}

			if (localSSLSocket != null) {
				try {
					localSSLSocket.startHandshake();
				} catch (SSLException localSSLException) {
					Debug.out.println(localSSLException);
					i = 0;
					this.validateSSLCert = false;
				} catch (IOException localIOException3) {
					Debug.out.println(localIOException3);
					i = 0;
					this.validateSSLCert = false;
				}
				if (i == 0) {

					j = JOptionPane.showConfirmDialog(JViewerApp.getInstance().getConnectionDialog(),
							LocaleStrings.getString("Z_9_URLP") + LocaleStrings.getString("Z_10_URLP"),
							LocaleStrings.getString("Z_8_URLP"), 0, 2);

					if (j != 0) {
						try {
							localSSLSocket.close();
						} catch (IOException localIOException4) {
							localIOException4.printStackTrace();
						}
						return null;
					}
				}
			}
		}
		if (!this.validateSSLCert) {
			if (paramInt2 == 1) {
				this.webSSLVerfiy = this.validateSSLCert;
			} else {
				this.kvmSSLVerify = this.validateSSLCert;
			}
			localSSLSocketFactory1 = getSocketFactory();

			try {
				localSSLSocket = (SSLSocket) localSSLSocketFactory1.createSocket();

				localSSLSocket.connect(new InetSocketAddress(paramInetAddress, paramInt1), 900);
			} catch (UnknownHostException localUnknownHostException1) {
				Debug.out.println(localUnknownHostException1);
				localSSLSocket = null;
			} catch (IOException localIOException1) {
				Debug.out.println(localIOException1);
				localSSLSocket = null;
			}
		}

		if (localSSLSocket == null) {
			this.connErrCode = SOCK_CREAT_FAILED;

			if ((JViewerApp.getInstance().getConnectionDialog() != null)
					&& (!JViewerApp.getInstance().getRetryConnection())) {
				JOptionPane.showMessageDialog(JViewerApp.getInstance().getConnectionDialog(),
						LocaleStrings.getString("S_11_SACD"), LocaleStrings.getString("S_9_SACD"), 0);

			} else if (!JViewerApp.getInstance().getRetryConnection()) {
				JOptionPane.showMessageDialog(JViewer.getMainFrame(), LocaleStrings.getString("S_11_SACD"),
						LocaleStrings.getString("S_9_SACD"), 0);
			}
		}

		return localSSLSocket;
	}

	private Socket createNonSSLSocket(InetAddress paramInetAddress, int paramInt) {
		Socket localSocket = null;

		try {
			localSocket = new Socket();

			localSocket.connect(new InetSocketAddress(paramInetAddress, paramInt), 900);
		} catch (Exception e) {
			this.connErrCode = SOCK_CREAT_FAILED;
			Debug.out.println(e);
			e = null;
		}
		return localSocket;
	}

	public Socket createSocket(InetAddress paramInetAddress, int paramInt1, int paramInt2) {
		Object localObject = null;

		this.connErrCode = SUCCESS;
		if (paramInt2 > 0) {
			localObject = createSSLSocket(paramInetAddress, paramInt1, paramInt2);
		} else if (paramInt2 == 0)
			localObject = createNonSSLSocket(paramInetAddress, paramInt1);
		return (Socket) localObject;
	}

	public boolean isWebSSLVerify() {
		return this.webSSLVerfiy;
	}

	public void setWebSSLVerify(boolean paramBoolean) {
		this.webSSLVerfiy = paramBoolean;
	}

	public boolean isKvmSSLVerify() {
		return this.kvmSSLVerify;
	}

	public void setKvmSSLVerify(boolean paramBoolean) {
		this.kvmSSLVerify = paramBoolean;
	}

	public int getConnErrCode() {
		return this.connErrCode;
	}
}
